package com.trekmapper.server.country;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.Extent;
import javax.jdo.PersistenceManager;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.trekmapper.client.country.Country;
import com.trekmapper.client.country.CountryService;
import com.trekmapper.server.PMF;

/**
 * The server side implementation of the RPC service.
 * 
 * @author Daniel Jodar
 */
public class CountryImpl extends RemoteServiceServlet implements CountryService {

	private static final long serialVersionUID = 1L;
	
	@Override
	public String insert(String country) {
		Country entity = new Country();
		entity.setName(country);
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		pm.makePersistent(entity);
		
		return "Thanks for creating " + country;
	}

	@Override
	public List<Country> find() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Extent<Country> extent = pm.getExtent(Country.class);
//	    Query query = pm.newQuery(Country.class);
//	    query.setOrdering("name DESC");
		List<Country> list = new ArrayList<Country>();
		for(Country country : extent){
			list.add(country);
		}
//	    List<Country> countries = (List<Country>) query.execute();
	    return list;
	}

}
